﻿@inject IProjectService ProjectService
<div align="center">
    <label>@_title</label>
</div>
<Chart @ref="_pieChart" ChartType="ChartType.Pie" OnInitAsync="@OnInit" Height=300px />
@* 居中显示按钮组合 *@
<div class="btn-group d-flex justify-content-center">
    <button class="btn btn-success">
        <input class="form-check-input" type="checkbox" value="@_annual" @onchange="OnYtdChange">
        <label class="form-check-label ms-2">
            YTD
        </label>
    </button>
    <button class="btn btn-primary" @onclick="@OnPreMonth">@("<")</button>
    <button class="btn btn-primary" @onclick="@OnNextMonth">@(">")</button>
    <button class="btn btn-success">
        <input class="form-check-input" type="checkbox" value="@_timerEnable" @onchange="OnTimerEnableChange" checked="checked">
        @if (_timerEnable)
        {
            <label class="form-check-label ms-2">Loop On</label>
        }
        else
        {
            <label class="form-check-label ms-2">Loop Off</label>
        }
    </button>
</div>
@code {
    private Chart _pieChart = default!;
    private List<ChartIntDataDto> chartDataDtos = new();
    private string _title = string.Empty;
    private int _year;
    private int _month;
    private bool _annual;

    private bool _timerEnable;
    System.Timers.Timer _timer;//定时触发

    protected override void OnInitialized()
    {
        _year = DateTime.Today.Year;
        _month = DateTime.Today.Month;
        _timerEnable = true;
        //初始化定时器
        _timer = new System.Timers.Timer(20000);//间隔20s
        _timer.Elapsed += (sender, eventArgs) => OnTimerCallback();
        _timer.Start();
    }

    private async Task<ChartDataSource> OnInit()
    {
        chartDataDtos = await ProjectService.GetBlazorProjectRiskLevelsAsync(_year, _month, _annual);
        var ds = new ChartDataSource();
        _title = _annual ? $"{_year}年YTD项目风险等级统计，项目总计{chartDataDtos.Sum(x => x.Value)}" : $"{_year}年{_month}月项目风险等级统计，项目总计{chartDataDtos.Sum(x => x.Value)}";

        //ds.Options.Title = "";
        ds.Options.ShowDataLabel = true;
        //ds.Options.LegendPosition = ChartLegendPosition.Right;
        ds.Labels = chartDataDtos.Select(x => x.Label);
        ds.Data.Add(new ChartDataset()
            {
                Label = "数量",
                Data = chartDataDtos.Select(x => x.Value).Cast<object>(),
                // BackgroundColor = Charts.Utility.StatusColors,//无法自定义饼图颜色
                // BorderColor = Charts.Utility.StatusColors
            });
        StateHasChanged();
        return ds;
    }

    private async void OnYtdChange(ChangeEventArgs e)
    {
        _annual = Convert.ToBoolean(e.Value);
        await _pieChart.Update(ChartAction.Update);
    }

    private async void OnPreMonth()
    {
        if (_month <= 1) return;
        _month -= 1;
        await _pieChart.Update(ChartAction.Update);
    }

    private async void OnNextMonth()
    {
        if (_month > 11) return;
        _month += 1;
        await _pieChart.Update(ChartAction.Update);
    }

    private void OnTimerEnableChange(ChangeEventArgs e)
    {
        if (_timerEnable)
        {
            _timer.Stop();//停止计时器
        }
        else
        {
            _timer.Start();//启动计时器
        }
        _timerEnable = !_timerEnable;
    }
    //计时器执行的操作
    //三个状态循环，60s，20s一次
    //状态1.当月，状态2.下一个月，状态3.YTD
    private int _state = 1;
    public void OnTimerCallback()
    {
        _ = InvokeAsync(() =>
         {
             //当前状态，显示下一个状态
             _state++;
             if (_state == 4) _state = 1;
             switch (_state)
             {
                 case 1:
                     _annual = false;
                     _month= DateTime.Today.Month;
                     break;
                 case 2:
                     _annual = false;
                     _month= DateTime.Today.Month+1;
                     break;
                 case 3:
                     _annual = true;
                     break;
             }
             _pieChart.Update(ChartAction.Update);
             StateHasChanged();
         });
    }
}
